package com.raptor.rabbitmq.product;

import com.raptor.rabbitmq.common.RabbitConstant;
import com.raptor.rabbitmq.common.Send;
import com.raptor.rabbitmq.exception.RabbitMQException;
import com.shihang.core.base.util.ObjectUtil;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ProducetSpringProvider implements InitializingBean, DisposableBean {


    private Send send;

    @Autowired
    private RabbitMQProductProperties rabbitMQProductProperties;


    @Override
    public void destroy() throws Exception {

    }

    @Override
    public void afterPropertiesSet() throws Exception {

        String exchange = rabbitMQProductProperties.getExchange();
        switch (exchange.toUpperCase()) {
            case RabbitConstant.CUSTOMER:
                send = ProductExchangeApplocationContextAware.map.get(RabbitConstant.CUSTOMER);
                break;
            case RabbitConstant.DIRECT:
                send = ProductExchangeApplocationContextAware.map.get(RabbitConstant.DIRECT);
                break;
            case RabbitConstant.FANOUT:
                send = ProductExchangeApplocationContextAware.map.get(RabbitConstant.FANOUT);
                break;
            default:
                send = ProductExchangeApplocationContextAware.map.get(RabbitConstant.TOPIC);
                break;
        }


    }

    public void product(String msg) throws RabbitMQException {
        ObjectUtil.checkArgument(msg, send);
        send.send(null);
    }


}
